package me.linkcn.ydtxl.client.util;

import java.io.InputStream;

import me.linkcn.ydtxl.client.R;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.CharArrayBuffer;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.QuickContactBadge;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class ContactListItemAdapter extends ResourceCursorAdapter {
	
	static final int SUMMARY_ID_COLUMN_INDEX = 0;
	static final int SUMMARY_NAME_COLUMN_INDEX = 1;
	static final int SUMMARY_STARRED_COLUMN_INDEX = 2;
	static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;
	static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;
	static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;
	static final int SUMMARY_LOOKUP_KEY = 6;
	static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;
	
	public ContactListItemAdapter(Context context, int layout, Cursor c) {
		super(context, layout, c);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final ContactListItemCache cache = (ContactListItemCache) view.getTag();
		final Context c = context;
		// Set the name
		cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
		int size = cache.nameBuffer.sizeCopied;
		cache.nameView.setText(cache.nameBuffer.data, 0, size);
		final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
		final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
		cache.photoView.assignContactUri(Contacts.getLookupUri(contactId,
				lookupKey));
		final Uri contactUri = ContentUris.withAppendedId(
				ContactsContract.Contacts.CONTENT_URI, contactId);
		InputStream input = ContactsContract.Contacts
				.openContactPhotoInputStream(context.getContentResolver(), contactUri);
		if (null != input) {
			cache.photoView.setImageBitmap(BitmapFactory.decodeStream(input));
		} else {
			cache.photoView.setImageResource(R.drawable.ic_contact_picture);
		}

		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
				c.startActivity(intent);
			}

		});
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = super.newView(context, cursor, parent);
		ContactListItemCache cache = new ContactListItemCache();
		cache.nameView = (TextView) view.findViewById(R.id.name);
		cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
		view.setTag(cache);

		return view;
	}    
    
    final static class ContactListItemCache {
        public TextView nameView;
        public QuickContactBadge photoView;
        public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
    }
}